// Program by Matthew Ettles 2011
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;


public class Window extends JFrame {


    private Puzzle puzzleWindow = new Puzzle();



    //Setting up variables relating to "Possible Assignment Structure video"
    private JTextField textField;
    private JButton playButton;
    private JLabel timerLabel;
    private JLabel movesLabel;
    // extra label I decided to add
    private JLabel nameLabel;
    // decided to make the 16th button seperate
    private JButton buttonSixteen;

    // timer variables
    int moveCount = 0;
    int counter;
    static Timer timer;
    
    // 2d Array variables
    static final int ROWS = 4;
    static final int COLS = 4;

    private static final int CELL_SIZE = 80;

    JButton[][] grid = new JButton[ROWS][COLS];






    public Window(){
        // standard setup
        setTitle("15 Game Assignment 1 - CP1300");
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);


        // experimenting with grid layout, decided on 2 panels
        // setLayout(new GridLayout(5, 4));


        // create the containers - two panels TOP and CENTER
        JPanel topPanel = new JPanel();
        topPanel.setBackground(new Color(239,228,176));
        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(Color.WHITE);


        // added center panel seperate gridLayout, WORKED PERFECTLY :-)
        centerPanel.setLayout(new GridLayout(4,4));
        // removed

        // add the panels to the frame
        add(topPanel, BorderLayout.NORTH);
        add(centerPanel, BorderLayout.CENTER);

        // create the UI elements
        textField = new JTextField(10); // for name entering

        timerLabel = new JLabel("Time Elapsed: " + counter); // for timer
        movesLabel = new JLabel("Total Moves: " + moveCount); // for moves counter
        nameLabel = new JLabel("Enter Name:");

        playButton = new JButton("Play! :-)");
        playButton.addActionListener(new NewGameT());





        buttonSixteen = new JButton("");





        // add buttons to the 2d array and display
        //grid = new JButton[COLS][ROWS];
         for (int r=0; r<ROWS; r++) {
                for (int c=0; c<COLS; c++) {
                    int x = c * CELL_SIZE;
                    int y = r * CELL_SIZE;
                    puzzleWindow.board[COLS][ROWS] = new JButton();
                    centerPanel.add(grid[COLS][ROWS]);
                    String text = puzzleWindow.getPiece(r, c);
                    if (text != null) {
                        
                    }


                
                
                //this.addMouseListener((MouseListener) this);
                //String text = puzzleWindow.getFace(r, c);
                
            }
        }



        // creating grid of buttons
        // vvvvvvvvvvvvvvvvvvvv for loop for buttons goes here
        //for (int i = 0; i < ROWS; ++i) {
        //    for (int j = 0; i < COLS; j++){
        //        grid[ROWS][COLS] = new JButton("" + (i + 1));
        //        centerPanel.add(grid);
        //    }
        //}
        
        // ^^^^^^^^^^^^^^^^^^^^

        // add the UI elements to the Panels
        topPanel.add(playButton);
        playButton.addActionListener(new NewGameT());
        topPanel.add(nameLabel);
        topPanel.add(textField);
        topPanel.add(timerLabel);
        topPanel.add(movesLabel);

        //centerPanel.add(buttonSixteen);


        counter = 0;
        timer = new Timer(1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                ++counter;
                timerLabel.setText("Time Elapsed: " + counter);
            }
        });

    }



    // dont actually need this anymore, using action listener below
//    ActionListener listener2 = new ActionListener() {
//
//
//                public void actionPerformed(ActionEvent e) {
//                  counter = 0;
//                  Window.timer.start();
//                }
//            };





        public class NewGameT implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            puzzleWindow.scramble();
            counter = 0;
            Window.timer.start();
        }
    }//end inner class NewGameAction
// 
    public static void main(String[] args) {
         Window window = new Window();

         window.setVisible(true);
         

    }
}
